﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucAddMaterialCategory : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["Purchase_SearchCategory"] = string.Empty;
            Search();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Purchase_SearchCategory"] = txtSearch.Text;
        Search();
    }
   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["Purchase_CategoryAdd/Edit"] = "Add";
        tblSearch.Visible = false;
        tblAdd.Visible = true;
        lblMaterialTitle.Text = "ADD MATERIAL CATEGORY";
        btnSave.Text = "Save";        
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        CheckSelection();
        Session["Purchase_CategoryAdd/Edit"] = "Edit";
        tblSearch.Visible = false;
        tblAdd.Visible = true;
        lblMaterialTitle.Text = "EDIT MATERIALS";
        btnSave.Text = "Update";
        LoadData();
        //FillDDL();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        tblSearch.Visible = true;
        tblAdd.Visible = false;
        txtCategory.Text = string.Empty;
        gvwVendorAdd.Visible = false;
        Search();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Validate())
        {       
            if (Session["Purchase_CategoryAdd/Edit"].ToString() == "Add")
                Save();
            else if (Session["Purchase_CategoryAdd/Edit"].ToString() == "Edit")
            {
                Edit();
                txtCategory.Text = string.Empty;
                tblSearch.Visible = true;
                tblAdd.Visible = false;
                Search();
            }
            if (Session["Purchase_AddVendor"].ToString() == "1")
            {
                AddVendor();
                Session["Purchase_AddVendor"] = "0";
            }
        }
    }

    void Search()
    {
        DataSet dsMaterials = new DataSet();
     //   _products.SearchMaterialCategory(Session["Purchase_SearchCategory"].ToString(), out dsMaterials);
        gvwMaterialCategory.DataSource = dsMaterials;
        gvwMaterialCategory .Columns[1].Visible = true;
        gvwMaterialCategory.DataBind();
        gvwMaterialCategory.Columns[1].Visible = false;
    }

    void LoadData()
    {
        string condition = "CategoryID = '" + Session["Purchase_CategoryID"].ToString() + "'";
        DataSet dsCategoryEdit = _products.GetData("MaterialCategory", condition);
        txtCategory.Text = dsCategoryEdit.Tables[0].Rows[0]["CategoryName"].ToString();       
    }

    //void FillDDL()
    //{
    //    try
    //    {           
    //        //fill vendor
    //        dsVendorCategory.Clear();
    //        dsVendorCategory = _products.GetData("VendorDetails", "");
    //        ddlVendor.DataSource = dsVendorCategory;
    //        ddlVendor.DataTextField = "VendorName";
    //        ddlVendor.DataValueField = "VendorID";
    //        ddlVendor.DataBind();
    //        ddlVendor.Items.Insert(0, "Select");
    //    }
    //    catch (Exception exp)
    //    {
    //        Session["ERRTITLE"] = "Fill DropDownList Failed";
    //        Session["ERRDET"] = exp.Message;
    //        Response.Redirect("ErrorPage.aspx");
    //    }

    //}

    void Save()
    {
        try
        {
            //if (_products.AddMaterialCategory(txtCategory.Text))
            //{
            //    Response.Write("<Script>alert('Added Successfully !')</Script>");
            //    txtCategory.Text=string.Empty;
            //}
            //else
            //    Response.Write("<Script>alert('Already Exist !')</Script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Add Materials Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void Edit()
    {
        string name, id;
        id = Session["Purchase_CategoryID"].ToString();
        name=txtCategory.Text;
//        _products.EditMaterialCategory(name, id);
    }

    void AddVendor()
    {
        string categoryID,vendorID,status;
        categoryID = string.Empty;
        status = Session["Purchase_CategoryAdd/Edit"].ToString();
        if (status == "Edit")        
            categoryID = Session["Purchase_CategoryID"].ToString();
        vendorID = Session["Purchase_VendorID"].ToString();
        string rank =Session["Purchase_Rank"].ToString();
       
//        _products.AssignVendor(categoryID, vendorID, rank, status);
              
    }

    
    protected void lnbAddVendor_Click(object sender, EventArgs e)
    {
        Session["Purchase_AddVendor"] = "1";
        gvwVendorAdd.Visible = true;
        FillGrid();
    }

    void FillGrid()
    {
        DataSet dsVendor = _products.GetData("VendorDetails", "");
        gvwVendorAdd.DataSource = dsVendor;
        gvwVendorAdd.DataBind();
    }

    /// <summary>
    /// check which rows of the grid view are selected
    /// </summary>
    /// <returns></returns>
    void CheckSelection()
    {
        try
        {
            int count = 0;
            int index = 0;

            for (int i = 0; i < gvwMaterialCategory.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwMaterialCategory.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    index = i;
                    count++;
                }
            }

            Session["Purchase_CategoryID"] = gvwMaterialCategory.Rows[index].Cells[1].Text;

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Check Selection Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    bool Validate()
    {
    try
        {
            for (int i = 0; i < gvwVendorAdd.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwVendorAdd.Rows[i].Cells[0].FindControl("chkEditDelete"));
                DropDownList ddlGrid = (DropDownList)(gvwVendorAdd.Rows[i].Cells[3].FindControl("ddlRank"));
                if ((chkGrid.Checked == true) && (ddlGrid.SelectedIndex == 0))
                {
                    Response.Write("<script>alert('Select Priority')</script>");
                    return false;
                }
                else if ((chkGrid.Checked == true) && (ddlGrid.SelectedIndex != 0))
                {
                    Session["Purchase_VendorID"] = gvwVendorAdd.Rows[i].Cells[1].Text;
                    Session["Purchase_Rank"] = ddlGrid.SelectedItem.ToString();
                }
                for (int j = i + 1; j < gvwVendorAdd.Rows.Count; j++)
                {
                    CheckBox chkGrid2 = (CheckBox)(gvwVendorAdd.Rows[j].Cells[0].FindControl("chkEditDelete"));
                    DropDownList ddlGrid2 = (DropDownList)(gvwVendorAdd.Rows[j].Cells[3].FindControl("ddlRank"));
                    if ((chkGrid2.Checked) && (ddlGrid.SelectedIndex == ddlGrid2.SelectedIndex))
                    {
                        Response.Write("<script>alert('Vendors Should Be Of Different Priority')</script>");
                        return false;
                    }
                }

            }         

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Validation Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    return true;

    }
    

    protected void gvwVendorAdd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlGrid = (DropDownList)e.Row.FindControl("ddlRank");
           
            ddlGrid.Items.Insert(0, "Select");
            ddlGrid.Items.Add("1");
            ddlGrid.Items.Add("2");
            ddlGrid.Items.Add("3");
            ddlGrid.Items.Add("4");
            ddlGrid.Items.Add("5");
            ddlGrid.Items.Add("6");
           
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";

        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

   

}
